﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.DB.ExtensibleStorage;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using YArchitech.Controls;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	internal class QuickLocateUpdater : IUpdater
	{
		public QuickLocateUpdater(AddInId addinId)
		{
			this.m_updaterId = new UpdaterId(addinId, new Guid("6B8C1A6B-C30A-43BF-9694-84F03E63A331"));
		}

		public void Execute(UpdaterData data)
		{
			string text = string.Empty;
			string empty = string.Empty;
			string value = string.Empty;
			string arg = string.Empty;
			string key = string.Empty;
			string text2 = string.Empty;
			Curve curve = null;
			Document document = data.GetDocument();
			try
			{
				foreach (ElementId elementId in data.GetModifiedElementIds())
				{
					Element element = document.GetElement(elementId);
					Schema schema = Schema.Lookup(new Guid("720080CB-DA99-40DC-9415-E53F280AA1F0"));
					if (schema != null)
					{
						Entity entity = element.GetEntity(schema);
						if (entity != null && entity.Schema != null)
						{
							IDictionary<string, string> dictionary = entity.Get<IDictionary<string, string>>("filterMark");
							if (dictionary != null && dictionary.Count > 0)
							{
								text = dictionary.Keys.FirstOrDefault<string>();
								if (!string.IsNullOrEmpty(text) && !(text.Split(new char[]
								{
									';'
								})[0] != elementId.IntegerValue.ToString()))
								{
									value = text.Split(new char[]
									{
										';'
									})[1];
									XYZ xyz = this.StringToXYZ(value);
									text2 = dictionary[text];
									if (element is Pipe || element is Duct || element is CableTray || element is Wall)
									{
										curve = (element.Location as LocationCurve).Curve;
									}
									else if (element is Grid)
									{
										curve = (element as Grid).Curve;
									}
									if (!(curve == null))
									{
										XYZ xyz2 = (curve.GetEndPoint(0) + curve.GetEndPoint(1)) * 0.5;
										XYZ moveVector = xyz2.Subtract(xyz);
										string[] array = text2.Split(new char[]
										{
											','
										});
										for (int i = 0; i < array.Length; i++)
										{
											ElementId elementId2 = new ElementId(Convert.ToInt32(array[i]));
											Element element2 = document.GetElement(elementId2);
											new YJKRotateFamilyInstance().MoveElement(moveVector, element2);
										}
										IDictionary<string, string> dictionary2 = new Dictionary<string, string>();
										arg = QuickMarkManager.PointToString(xyz2);
										key = string.Format("{0};{1}", element.Id.IntegerValue.ToString(), arg);
										dictionary2.Add(key, dictionary[text]);
										QuickMarkManager.StoreData(element, dictionary2, false);
										document.Regenerate();
									}
								}
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				YJKMessageBox.Show(ex.Message);
			}
		}

		public string GetAdditionalInformation()
		{
			return "";
		}

		public ChangePriority GetChangePriority()
		{
            return (ChangePriority)(15);
		}

		public UpdaterId GetUpdaterId()
		{
			return this.m_updaterId;
		}

		public string GetUpdaterName()
		{
			return "";
		}

		private XYZ StringToXYZ(string value)
		{
			XYZ result = null;
			if (string.IsNullOrEmpty(value))
			{
				return result;
			}
			string[] array = value.Split(new char[]
			{
				','
			});
			double num = Convert.ToDouble(array[0]);
			double num2 = Convert.ToDouble(array[1]);
			double num3 = Convert.ToDouble(array[2]);
			return new XYZ(num, num2, num3);
		}

		private UpdaterId m_updaterId;
	}
}
